package cn.yougou.controller;

import cn.yougou.common.DtoUtil;
import cn.yougou.dto.Dto;
import cn.yougou.pojo.YgAddress;
import cn.yougou.service.user.YgUserAddressService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("ygAddress")
public class YgAddressController {

    /**
     * 服务对象
     */
    @Reference
    private YgUserAddressService ygUserAddressService;

    @GetMapping("selAddress/{id}")
    public Dto selAddress(@PathVariable Integer id){
        return DtoUtil.returnDataSuccess(ygUserAddressService.selAddress(id));
    }

    @PostMapping("addAddress")
    public Dto addAddress(@RequestBody YgAddress ygAddress){
        if (ygAddress.getDefaultid() == 1) {
            //1 去调用改变其他地址的默认开启
            int upone = ygUserAddressService.upzerofromone(ygAddress.getId());
            //2 改成功没
            if (upone > 0) {
                int add = ygUserAddressService.addAddress(ygAddress);
                if (add > 0) {
                    return DtoUtil.returnSuccess();
                } else {
                    return DtoUtil.returnFail("5002", "失败");
                }
            } else {
                //小于1
                return DtoUtil.returnFail("5002", "失败");
            }
        }else {
            ygUserAddressService.addAddress(ygAddress);
            return DtoUtil.returnSuccess();
        }
    }

    @PostMapping("upAddress")
    public Dto updateAddress(@RequestBody YgAddress ygAddress) {
        if (ygAddress.getDefaultid() == 1) {
            //1 去调用改变其他地址的默认开启
            int upone = ygUserAddressService.upzerofromone(ygAddress.getId());
            //2 改成功没
            if (upone > 0) {
                int updated = ygUserAddressService.upAddress(ygAddress);
                if (updated > 0) {
                    return DtoUtil.returnSuccess();
                }else {
                    return DtoUtil.returnFail("5002","失败");
                }
            } else {
                //小于1
                return DtoUtil.returnFail("5002", "失败");
            }
        }else {
            System.out.println(ygAddress.getAddressId());
            System.out.println(ygAddress.getDefaultid());
            int update = ygUserAddressService.upAddress(ygAddress);
            if (update > 0) {
                return DtoUtil.returnSuccess();
            }else {
                return DtoUtil.returnFail("5002","失败");
            }
        }
    }

        @PostMapping("del/{addressId}")
        public Dto deleteAddress ( @PathVariable("addressId") Integer addressId){
            return  (ygUserAddressService.deAddress(addressId) > 0)
                    ?
                    DtoUtil.returnSuccess()
                    :
                    DtoUtil.returnFail("5002,","删除失败");
        }

        @GetMapping("selMo")
        public YgAddress selMo(Integer id){
           return ygUserAddressService.selMo(id);
        }
}
